﻿using System.Collections.ObjectModel;
using System.Linq;
using Caliburn.PresentationFramework.Filters;
using CI.Models;
using CI.Models.Data;
using CI.Models.Production;
using CI.ViewModels.Diagramming;
using CI.ViewModels.Interfaces;

namespace CI.ViewModels
{
    public abstract class ItemSelectorViewModelBase : ModelBase, IItemSelectorViewModel
    {
        private readonly ObservableCollection<object> _items = new ObservableCollection<object>();
        private string _filterByName;
        private object _selectedItem;
        public string GroupName { get; protected set; }
        

        private bool _canClearFilter;
        private bool _canAddItem;

        protected ItemSelectorViewModelBase(string groupName,
                                            IDiagramViewModel parentViewModel)
        {
            GroupName = groupName;
            StartModel();
        }

        public ObservableCollection<object> Items
        {
            get { return _items; }
        }

        public object SelectedItem
        {
            get { return _selectedItem; }
            set
            {
                SetField(ref _selectedItem, value, () => SelectedItem);
            }
        }

        public string FilterByName
        {
            get { return _filterByName; }
            set
            {
                if (SetField(ref _filterByName, value, () => FilterByName))
                {
                    ApplyFilter();
                    CanClearFilter = !string.IsNullOrEmpty(FilterByName);
                }
            }
        }

        protected abstract void ApplyFilter();

        public bool CanClearFilter
        {
            get { return _canClearFilter; }
            set { SetField(ref _canClearFilter, value, () => CanClearFilter); }
        }

        [Preview("CanClearFilter", AffectsTriggers = true)]
        public void ClearFilter()
        {
            FilterByName = string.Empty;
        }
    }
}